// pagesProperty/pages/checkMeter/add/index.js
var app = getApp(), fetchApi = require("../../../../utils/httpPost.js"), common_Js = require("../../../../component/common.js")
import Dialog from '../../../../component/vant/dialog/dialog';
import Toast from '../../../../component/vant/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    roomId:0,
    isLoading:true,
    lastWaterTemp:{},
    lastElectricTemp:{},
    fee_type:'eletrict',
    is_garage:0,
    check_year:'',
    check_month:'',
    waterSinglePrice:0,
    waterBusinessSinglePrice:0,
    electricSinglePrice:0,
    electricBusinessSinglePrice:0,
    submitState:false,
    beginCount:0,
    endCount:0,
    roomType:0,
    roomTypeTxt:'',
    roomNo:'',
    estateName:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(options.room_id){
      this.data.roomId = parseInt(options.room_id);
    }
    this.onHttpInit();
  },
  
  onHttpInit: function () {
    var that = this
    fetchApi.httpPost('/estate/room/template/info', {
      room_id: that.data.roomId,
      fee_type:that.data.fee_type,
      is_garage: that.data.is_garage,
    }, function (res) {
      console.log("获取二维码数据", res)
      if (res.data.code == 0) {
        that.data.lastWaterTemp = res.data.data.waterTemp;
        that.data.lastElectricTemp = res.data.data.electricTemplate;
        that.setData({ 
          lastWaterTemp:res.data.data.waterTemp,
          lastElectricTemp:res.data.data.electricTemplate,
          check_year: res.data.data.check_year,
          check_month: res.data.data.check_month,
          waterSinglePrice: res.data.data.waterSinglePrice,
          waterBusinessSinglePrice: res.data.data.waterBusinessSinglePrice,
          electricSinglePrice:res.data.data.electricSinglePrice,
          electricBusinessSinglePrice:res.data.data.electricBusinessSinglePrice,
          roomType: res.data.data.roomType,
          roomTypeTxt: res.data.data.roomTypeTxt,
          roomNo: res.data.data.roomNo,
          estateName: res.data.data.estateName,
        })
      }else{
        Toast.fail(res.data.msg)
      }
      that.setData({ netWork: true, isLoading: false, })
    }, function () {
      that.setData({ netWork: false, hasData: false, isLoading: false, })
    })
  },
  inputHandle(e){
    var type = e.currentTarget.dataset.type;
    console.log('inputMoney',e);
    var money = e.detail;
    if(money == ""){
      money = "0";
    }
    let value = e.detail;
    if (value != '') {
      value = value.replace(/[^\d.]/g, "")
      if (/^(\d?)+(\.\d{0,2})?$/.test(value)) {} else {
        value = value.substring(0, value.length - 1);
      }
      value = value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3')
      if (value.indexOf(".") < 0 && value != "") {
        value = parseFloat(value);
      }
    }

    if(type == 'begin'){
      console.log('value',value)
      this.data.beginCount = value;
      this.setData({
        beginCount: value
      })
    }else{
      console.log('value',value)
      this.data.endCount = value;
      this.setData({
        endCount: value
      })
    }
  },
  onChangeFeeType(e){
    console.log('onChangeFeeType',e);
    this.data.fee_type = e.detail;
    this.setData({
      fee_type: e.detail
    })

    console.log('this.data.lastWaterTemp',this.data.lastWaterTemp)
    console.log('this.data.lastElectricTemp',this.data.lastElectricTemp)
    if(this.data.fee_type == 'water'){
      if(this.data.lastWaterTemp !=undefined){
        this.data.beginCount = this.data.lastWaterTemp.endCount;
      }else{
        this.data.beginCount = 0;
      }

    } else if(this.data.fee_type == 'eletrict'){
      if(this.data.lastElectricTemp !=undefined){
        this.data.beginCount = this.data.lastElectricTemp.endCount;
      }else{
        this.data.beginCount = 0;
      }
    }
    console.log('this.data.beginCount',this.data.beginCount)
    this.setData({
      beginCount: this.data.beginCount
    })
  },
  onChangeGarage(e){
    console.log('onChangeGarage',e);
    this.data.is_garage = e.detail;
    this.setData({
      is_garage: e.detail
    })
  },
  submitOp(){
    var that = this;
    if(this.data.check_year == 0 || this.data.check_month == 0){
      Toast('请后台配置抄表月份');
      return;
    }


    if(this.data.endCount < this.data.beginCount){
      Toast('止数不能小于起数');
      return;
    }

    if(this.data.beginCount == undefined || this.data.beginCount == ''){
      Toast('请输入本月起数');
      return;
    }

    if(this.data.endCount == 0){
      Toast('本月止数必须大于0');
      return;
    }

    if(that.data.submitState == true){
      Toast("不要操作太快哟~");
      return;
    }
    
    that.data.submitState = true;
    that.setData({
      submitState: true,
    })

    fetchApi.httpPost('/estate/add/room/template', {
      room_id:that.data.roomId,
      year_num: that.data.check_year,
      month_num:that.data.check_month,
      begin_count: that.data.beginCount,
      end_count: that.data.endCount,
      fee_type: that.data.fee_type,
      is_garage: that.data.is_garage,
    }, function (res) {
      console.log("提交水电表炒熟数据", res)
      if (res.data.code == 0) {
        Toast.success(res.data.msg);
        that.data.submitState = false;
        that.setData({
          submitState: false,
        })
        // setTimeout(function(){
        //   wx.switchTab({
        //     url: '/pages/personal/index',
        //   })
        // },500);
      } else {
        Toast.fail(res.data.msg);
        that.data.submitState = false;
        that.setData({
          submitState: false,
        })
      }
      that.setData({
        isLoading: false, loading: true, netWork: true,
      });
    }, function () { })
  },
})